package com.gxufl.dao;

import java.util.List;

import com.gxufl.entity.Notice;
import com.gxufl.entity.Room;
import com.gxufl.entity.Seat;
import com.gxufl.entity.Ticket;
import com.gxufl.util.JdbcUtil;

public class SeatDao {

	/**
	 * 查询方法
	 * 
	 * @param name
	 * @param showdate
	 * @return
	 */
	public List<Seat> findList(String roomname, Boolean isuse) {
		String sql = "select * from movie_seat where 1";

		if (roomname != null && !roomname.isEmpty()) {
			sql = sql + " and roomname = '" + roomname + "'";
		}

		if (isuse != null) {
			sql += " and isuse = " + (isuse ? 1 : 0);
		}

		return JdbcUtil.query(sql, Seat.class);
	}

	public Seat findOneByCode(String code) {
		String sql = "select * from movie_seat where 1 and code = '" + code
				+ "'";
		
		List<Seat> list = JdbcUtil.query(sql, Seat.class);
		if(!list.isEmpty()) {
			return list.get(0);
		}
		return null;
//		return JdbcUtil.queryOne(sql, Seat.class);
	}

	/**
	 * 保存方法
	 * 
	 * @param room
	 */
	public void save(List<Seat> list) {

		boolean isOK = true;

		for (int i = 0, ii = list.size(); i < ii; i++) {
			isOK = isOK && JdbcUtil.save(list.get(i));
		}

		if (isOK) {
			System.out.println("保存成功");
		} else {
			System.out.println("保存失败");
		}
	}

	public void save(Seat seat) {
		boolean isOK = JdbcUtil.save(seat);

		if (isOK) {
			System.out.println("保存成功");
		} else {
			System.out.println("保存失败");
		}
	}

	/**
	 * 在保存票据前，验证票据有效性的逻辑
	 * 返回true,表示验证通过，返回false表示不通过
	 */
	public boolean checkBeforeSaveTictet(Ticket ticket) {
		String seatcode = ticket.seatcode;

		Seat seat = findOneByCode(seatcode);

		if (seat != null) {
			
			if (!seat.isuse) {
				return true;
			}
		}

		return false;
	}

}
